package org.nemanjaristic.database.managers;

import org.hibernate.Session;
import org.nemanjaristic.database.HibernateUtil;
import org.nemanjaristic.metamodel.beans.UserBean;

public class ManagerFactoryImpl extends ManagerFactory {

	@SuppressWarnings("unchecked")
	private HibernateEntityManager createManager(Class entityClass) {
		try {
			HibernateEntityManager manager = (HibernateEntityManager) entityClass.newInstance();
			manager.setSession(getCurrentSession());

			return manager;
		}
		catch (Exception ex) {
			throw new RuntimeException("Can not create manager: " + entityClass, ex);
		}
	}

	protected Session getCurrentSession() {
		return HibernateUtil.getSessionFactory().getCurrentSession();
	}
	
	public static class UserManager extends HibernateEntityManager<UserBean, Long> {}

	@Override
	public UserManager getUserManager() {
		return (UserManager) createManager(UserManager.class);
	}

}
